﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Core;

#endregion

namespace MarketPulse.BusinessLogic.Common
{
    /// <summary>
    /// This class represents a collection of Accreditation objects
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.AccreditationFactory, MarketPulse.DataAccess.Factory", "FetchUserAccreditationList")]
    public class UserAccreditationList : MarketPulseListBase<UserAccreditationList, UserAccreditation>
    {

        #region Constructors

            /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// only via the static factory methods provided.
            /// </summary>
            private UserAccreditationList()
            {
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Method used to create a new collection of UserAccreditation objects via the DataPortal client proxy
                /// </summary>
                /// <returns>The newly created collection of visits</returns>
                public static UserAccreditationList New()
                {
                    return DataPortal.Create<UserAccreditationList>();
                }

                /// <summary>
                /// Method used to get a list of User Accreditation objects that are associated with a specific Accreditation
                /// </summary>
                /// <param name="accreditationGUID">The Guid of the Accreditation to retrieve the User Accreditation list for</param>
                /// <returns>The populated User Accreditaiton List collection</returns>
                public static UserAccreditationList Get(Guid accreditationGUID)
                {
                    return DataPortal.Fetch<UserAccreditationList>(new SingleCriteria<Guid>(accreditationGUID));
                }

            #endregion

            /// <summary>
            /// Method used to retrieve the number of attempts (by a specific user) to complete an Accreditation successfully
            /// </summary>
            /// <param name="accreditationGUID">The unique identifier for the accreditation in question</param>
            /// <param name="userGUID">The unique identifier for the user in question</param>
            /// <param name="result">Whether to look for positive or negative results</param>
            /// <returns>The number of attempts</returns>
            public int GetAttemptsByUser(Guid accreditationGUID, Guid userGUID, bool result)
            {
                var items = (from userAccreditation in this.Items
                              where userAccreditation.AccreditationGUID == accreditationGUID 
                                && userAccreditation.UserGUID == userGUID
                                && userAccreditation.Accredited == result
                              select userAccreditation);

                return items.Count();
            }

        #endregion

        #region Properties

            /// <summary>Methos used to retrieve a count of the attempts in this collection</summary>
            public new int Count { get { return this.Items.Count; } }

        #endregion
    }
}
